<template>
  <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick"  class="left_tree_tap">
    <div class="sjtj_ts"><i class="el-icon-warning-outline"></i>双击即添加</div>
    <el-tab-pane label="指标" name="first">
      <el-container>
        <el-header class="height_auto pdbt">
          <!-- <el-row :gutter="6" class="bk_padding_le_rg">
            <el-col :span="24">
              <label for="frang" style="width: 100px;font-size: 14px;">选择索引表</label>
            </el-col>
          </el-row> -->
          <el-row :gutter="6" class="bk_padding_le_rg">
            <el-col :span="24">
              <el-select v-model="tableID" placeholder="请选择索引表" size="mini" clearable :style="{width: '98%'}" @change="tableIDOnChange">
                <el-option v-for="(item, index) in tableIDOptions" :key="index" :label="item.name"
                  :value="item.id" :style="{width: '260px',higth:'500px'}"></el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-header>
        <el-main>
              <ps-tree
              ref="tbdwTree"
              :tree-comp-data="tbdwlTreeData"
              @createRightMenu="createRightMenu"
              @onTreeClick="treeClick"
              />
              
        </el-main> 
        <el-footer>
              <div class="xuanlexing">
                <el-container class="align_box">
            <el-main>
              <el-select v-model="value" placeholder="请选择下拉选择" size="mini" clearable class="select_box" >
                <el-option v-for="(item, index) in field181Options" :key="index" :label="item.label"
                  :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-main>
            <el-aside width="90px" style="line-height:20px;padding:0 0px 0 8px;">
              <el-button type="primary"  @click="sumClick" icon="el-icon-circle-plus-outline" round  plain  size="mini">添加</el-button>
            </el-aside>
          </el-container></div>
        </el-footer>
            </el-container>     
    </el-tab-pane>
    <el-tab-pane label="函数" name="second">
      <function-Selector @funClick="funClick" />
    </el-tab-pane>
  </el-tabs>
</template>
<script>
  import request from "@/utils/requestutil";
  import {getRealUrl, syncRequest} from "@/utils/httpRequet";
  import $ from "jquery";
  import treeExhide from "@/components/ztree/lib/jquery.ztree.exhide.min.js";
  import dicPanel from '@/hr/utils/dicPanel'
  import { options } from 'runjs';
  import psTree from '@/components/psTree/psTree';
  import { isNull } from 'xe-utils/methods';
  import functionSelector from '@/components/FunctionSelector/FunctionSelector'
  export default {
     components: {
      psTree,
      dicPanel,
      functionSelector,
    },
    data() {
      return {
        tableIDOptions: [],
        lable:"",
        ywValue:"",
        rwValue:"",
        tableID:"",
        treeParam:{},
        field181Options: [],
        value:"",
        orgschval: "",
        bzcode : "TBJJ",    
        tbdwlTreeData: {
          treeConfigKey:'BNSH_ZB_TREE',
          treeData: [],
          // treeConfigKey:'ZBGL_TI_INDEX_TREE',
          treeBuildConfig:{
            iconMap: {
              jg: "tree-icon jg",
              zbabc: "tree-icon zbabc",
            },
            beforeDrop: this.beforeDrop, 
            dealTreaparam:this.dealTreaparam,
            dataFilter:this.datafilter,
            beforeDblClick:this.beforeDblClick,
            beforeDrag:this.beforeDrag,
            isEdit:true,
            editShowType: 2, 
          }
        },
        activeName: 'first'
      };
    },
    props: {
      leftTreeData: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },
    watch:{
       leftTreeData: {
        handler(data) {
          //console.log("表间计算左侧树：", data);
          // this.$refs.tbdwTree.refresh();
          this.ywValue = data.ywValue
          this.rwValue = data.rwValue
          //this.rwValue = data.treeID
          this.refreshtableIDOptions(data.ywValue,data.rwValue,data.treeID);
        },
        deep: true,
      },
    },
    methods: {
      //添加按钮事件
      sumClick(){
          //console.log(this.value);
          this.$emit('sumClick', "\"" + this.value + "\"")
      },
      //函数双击事件
      funClick(val) {
        this.val = val
        this.$emit('hsClick', val)
      },
      handleClick(tab, event) {
        //console.log(tab);
      },
      createRightMenu(params)
      {
           params.items = []
      },
      datafilter(treeid,pnode,resdata){
        let t = this;
        let tree = [];
        if(resdata && resdata.data&& resdata.data.length>0){
          t.$_.forEach(resdata.data,(item,val)=>{
            if(item.leaf=='0')item.isParent=true
          });
        }else{
          if(pnode) pnode.isParent=false;
        }
        if (pnode == undefined) {
          //查询根节点
          if(t.ywValue != "" &&t.rwValue != "" && t.tableID !="")
          {
            let res = syncRequest({
              url:"bnshZbTreeData",
              method:"post",
              async: false, 
              data:{bzcode:t.ywValue,ttId:t.rwValue,tsCd:t.tableID}
            })
            //console.log("AAA:",res.data);
            let strTreeData = eval(res.data)
            //if(strTreeData.length > 0){
              tree =  strTreeData
            // }else{
            //    this.$message.warning("该表单下没有相关指标，请刷新或者联系管理员！");
            // }
          }
          t.$_.forEach(tree, (item, val) => {
            item.iconcls = "jg";
            if (item.children != undefined && item.children.length > 0) {
              t.$_.forEach(item.children, (strVal, val) => {
                strVal.iconcls = "zbabc";
              });
            }
          });
          resdata.data = tree;
        }
        return resdata.data;
      },
      dealTreaparam(treeid,node){
        const _that = this;
        if(node==undefined || node==''){    
          //return {p0:_that.ywValue,p1:_that.rwValue,p2:_that.tableID}
          return {p0:"ROOT",p1:"ROOT",p2:"ROOT"}
        }
      },
      beforeDrop(treeId, treeNodes, targetNode) {
          return false;
      },
      //指标双击事件
      beforeDblClick(treeId,treeNode)
      {
        //console.log(treeNode);
        if(treeNode && treeNode.tiICd && treeNode.tiICd != "undefined"){
          if (this.tableID && this.tableID != "" && treeNode.tiICd != "undefined") {
            let gs = ""
            gs += "@"+this.tableID+"@"+treeNode.tiICd
            this.$emit('zbClick', gs)
          }
          this.$emit('afterZbDblClick', treeNode)
        }
        
        return false;
      },
      beforeDrag() {
          return false;
      },
      treeClick(e, treeid, node){
        let _t = this;
        let tiIRtp = node.tiIRtp;
        if(isNull(tiIRtp) || tiIRtp == undefined || tiIRtp == ""){
           _t.field181Options=[];
           _t.value = "";
          return false;
        }else{
          request({
            url:"bnshXgflData",
            method:"post",
            params:{bzcode:_t.ywValue,tiIRtp:tiIRtp}
          }).then(function(data){
            //console.log(data.data);
            if(data.data.length > 0){
              _t.field181Options=data.data;
              _t.value = data.data[0].tcCtCd;
            }
          })
        }
      },
      //加载指标树
      // zbLodeTree(ywValue,rwValue,tableID){
      //   this.ywValue=ywValue;
      //   this.rwValue=rwValue;
      //   this.tableID=tableID;
      //   this.$refs.tbdwTree.refresh();
      // },
      refreshtableIDOptions(ywValue,rwValue,tableID) {
        let that = this
        //console.log(ywValue, rwValue, tableID);
        if (ywValue && rwValue) {
            let res = syncRequest({
              url:"dcbjs_tscd_sel",
              method:"post",
              async: false, 
              data:{ywValue:ywValue,rwValue:rwValue},
            })
            that.tableIDOptions = res.data
            that.tableID = tableID
            that.tbdwlTreeData.treeData=[]
            that.$refs.tbdwTree.refresh();
        }
      },
      tableIDOnChange(val) {
        console.log(val);
        this.tableID = val
        let obj =  this.tableIDOptions.find((item)=>{
         return item.id === val;
        });
        this.$refs.tbdwTree.refresh();
        //console.log(this.ywValue, this.rwValue, this.tableID);
      }
    }
  };
</script>
<style lang="scss" scoped>
.select_box{
  width:100%;
  padding-left: 8px;
}
.align_box{
  align-items: center;
  // border-top: 1px solid #dddddd;
}
.align_box .el-aside{
  background:#fff;
}
.align_box .el-main{
  overflow: hidden;
}
.el-input__suffix{
  top:-1px;
}
.tabl ::v-deep{ 
    .el-tabs__header {
      margin-bottom: 0px;
      height: 33px;
      .el-tabs__nav-scroll{ height: 33px;}
  }
}
</style>
